package com.yihu.wlyy.service.app.consult;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;

import com.yihu.wlyy.entity.consult.Consult;
import com.yihu.wlyy.repository.ConsultCommunicationDao;
import com.yihu.wlyy.repository.ConsultCommunicationReplyDao;
import com.yihu.wlyy.repository.ConsultDao;
import com.yihu.wlyy.repository.ConsultMoViDao;
import com.yihu.wlyy.repository.ConsultPublicDao;
import com.yihu.wlyy.repository.ConsultPublicReplyDao;
import com.yihu.wlyy.repository.ConsultTeamDao;
import com.yihu.wlyy.repository.DoctorDao;
import com.yihu.wlyy.repository.PatientDao;
import com.yihu.wlyy.service.BaseService;

public class ConsultService extends BaseService {

	@Autowired
	public PatientDao patientDao;
	@Autowired
	public DoctorDao doctorDao;
	@Autowired
	public ConsultDao consultDao;
	@Autowired
	public ConsultMoViDao consultMoViDao;
	@Autowired
	public ConsultTeamDao consultTeamDao;
	@Autowired
	public ConsultPublicDao consultPublicDao;
	@Autowired
	public ConsultPublicReplyDao consultPublicReplyDao;
	@Autowired
	public ConsultCommunicationDao consultCommunicationDao;
	@Autowired
	public ConsultCommunicationReplyDao consultCommunicationReplyDao;

	/**
	 * 添加一条咨询记录
	 * @param patient 患者信息
	 * @param title 咨询标题
	 * @param type 咨询类型：1三师咨询，2视频咨询，3图文咨询，4公共咨询，5病友圈
	 * @return
	 */
	public Consult addConsult(String patient, String title, String symptoms, String images, int type) {
		Consult consult = new Consult();
		consult.setCode(getCode());
		consult.setCzrq(new Date());
		consult.setDel("1");
		consult.setPatient(patient);
		consult.setTitle(title);
		consult.setSymptoms(symptoms);
		consult.setImages(images);
		consult.setType(type);
		return consultDao.save(consult);
	}

	/**
	 * 查询患者所有的咨询记录
	 * @param patient 患者标识
	 * @param id
	 * @param pagesize 分页大小
	 * @return
	 */
	public Page<Object> findConsultRecordByPatient(String patient, long id, int pagesize) {
		if (id < 0) {
			id = 0;
		}
		if (pagesize <= 0) {
			pagesize = 10;
		}
		// 排序
		Sort sort = new Sort(Direction.DESC, "id");
		// 分页信息
		PageRequest pageRequest = new PageRequest(0, pagesize, sort);
		if (id > 0) {
			return consultDao.findByPatient(patient, id, pageRequest);
		} else {
			return consultDao.findByPatient(patient, pageRequest);
		}
	}

}
